package com.lance.answer.section_import;

import com.lance.answer.util.CollectionsUtil;
import org.apache.commons.io.FileUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yixun
 * @version 2.0.0
 * @description
 * @date 2021-05-30 12:46
 * @since 2.0.0
 */
public class SectionImport {

    public static void main(String[] args) throws IOException {
        getSection();
    }

    public static void getSection() throws IOException {
        String url = "http://xinjiang.ouchn.cn/course/view.php?id=39";
        String userName = "2065001457729";
        String token = "8s85m20i0jol1oio81tr9lfdd6";
        Document doc = getDocument(url,userName,token);
        Elements courses = doc.getElementsByClass("course-content");
        //System.out.println(courses.toString());

        if (CollectionsUtil.isEmpty(courses)) {
            System.out.println("为空");
            return;
        }
        Element course = courses.get(0);
        int sectionCount = 100;
        List<String> lines = new ArrayList<>();
        for (int i = 2;i <= sectionCount;i++) {
            Element section = course.getElementById("section-" + i);
            //System.out.println(section.toString());
            if (null != section) {
                Elements instances = section.select("a[class=aalink]");
                for (Element instance:instances) {
                    String href = instance.attr("href");
                    String instanceName = instance.getElementsByClass("instancename").get(0).ownText();
                    String line = String.format("%s,%s",href,instanceName);
                    System.out.println(line);
                    lines.add(line);
                }
                System.out.println("\n");
                System.out.println("\n");
                System.out.println("-----------------------------------------");
            }
        }
        File file = new File("/Users/guiliangzhou/IdeaProjects/answer-tool/src/main/resources/00975/section.txt");
        FileUtils.writeLines(file,lines);


    }

    public static Document getDocument(String url,String userName,String token) {
        Map<String,String> cookies = new HashMap<>();
        cookies.put("MoodleSession",token);
        cookies.put("username",userName);

        Map<String,String> headers = new HashMap<>();
        headers.put("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9");
        headers.put("Accept-Language","zh-CN,zh;q=0.9,en;q=0.8");

        Document doc = null;
        try {
            doc = Jsoup.connect(url)
                    .userAgent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36")
                    .cookies(cookies)
                    .headers(headers)
                    .timeout(5000)
                    .get();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return doc;

    }

    public static Document getLmsDocument(String url,Map<String,String> cookies) {
//        Map<String,String> cookies = new HashMap<>();
//        cookies.put("session",token);
//        cookies.put("username",userName);

        Map<String,String> headers = new HashMap<>();
        headers.put("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9");
        headers.put("Accept-Language","zh-CN,zh;q=0.9,en;q=0.8");

        Document doc = null;
        try {
            doc = Jsoup.connect(url)
                    .userAgent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36")
                    .cookies(cookies)
                    .headers(headers)
                    .timeout(5000)
                    .get();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return doc;

    }




}
